package com.jp.knowledge.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.model.CommTags;

import java.util.List;

/**
 * Created by jiayen on 16/11/30.
 */

public class RightContentAdapter extends RecyclerView.Adapter<RightContentAdapter.Viewholder> implements  View.OnClickListener {

    private Context mContext;
    private List<CommTags> list;
    private TextPaint newPaint;
    private OnItemListerner  onItemListerner;
    public RightContentAdapter(Context context,List<CommTags> list) {
        this.list=list;
        this.mContext =context;

    }

    public void setOnItemListerner(OnItemListerner onItemListerner) {
        this.onItemListerner = onItemListerner;
    }

    public void bindViewHold(Viewholder holder, int position) {
        CommTags commTags = list.get(position);
        holder.name.setText(commTags.getTagName());
    }

    private double getWidth(String text){
        newPaint = new TextPaint();
        float textSize = mContext.getResources().getDisplayMetrics().scaledDensity * 16;
        newPaint.setTextSize(textSize);
        float newPaintWidth = newPaint.measureText(text);
        return newPaintWidth;
    }

    @Override
    public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        TextView view = (TextView) LayoutInflater.from(mContext).inflate( R.layout.comm_tags_item, parent, false);
        Viewholder  holder = new Viewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Viewholder holder, int position) {
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);
        bindViewHold(holder,position);
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    public void updataAdapter(List<CommTags> list){
        this.list = list;
        this.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        if(onItemListerner!=null)onItemListerner.onItemClick(v,position);
    }

    public static class Viewholder extends  RecyclerView.ViewHolder{
        private TextView  name;
        public Viewholder(TextView itemView) {
            super(itemView);
            name = itemView;
        }
    }

    /**
     * 获取标签
     * @return
     */
    public List<CommTags>  getCommTags(){
        return list;
    }
}
